import Vue from 'vue';
import VueRouter from 'vue-router';
import { getLocal } from '@/utils/utils';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      requireAuth: false,
    },
    component: () => import(/* webpackChunkName: "login" */ '../views/login/LoginView.vue'),

  },
  {
    path: '/regist',
    name: 'regist',
    component: () => import(/* webpackChunkName: "regist" */ '../views/regist/registView.vue'),

  },
  {
    path: '/restaurant',
    name: 'restaurant',
    meta: {
      requireAuth: true,
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "restaurant" */ '../views/restaurant/restaurantView.vue'),
  },
  {
    path: '/menu',
    name: 'menu',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "menu" */ '../views/menu/MenuView.vue'),
  },
  {
    path: '/order',
    name: 'order',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "order" */ '../views/order/OrderVi.vue'),
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    const user = getLocal('user');
    if (user) {
      next();
    } else {
      next('/login');
    }
  } else {
    next();
  }
});

export default router;
